package com.yulongtian.weekCompetition.month202212.week1;

/**
 * 可
 *
 * @author yulongTian
 * @create 2022-12-04 10:41
 */
public class Test02 {
    public static void main(String[] args) {
        int[] skill = {3, 2, 5, 1, 3, 4};
        System.out.println(dividePlayers(skill));
    }

    public static long dividePlayers(int[] skill) {
        //统计个数
        int[] count = new int[1001];
        //元素之和
        int len = skill.length;
        int sum = 0;
        for (int i : skill) {
            count[i]++;
            sum += i;
        }

        //每个团队技能点
        int skillAvg = sum / (len / 2);

        //化学反应之和
        long resSum = 0L;

        for (int num : skill) {
            int target = skillAvg - num;

            //负数或超过
            if (target < 0 || target >= 1001) {
                return -1;
            }


            //target还有
            if (count[target] > 0) {
                count[target]--;
                resSum += num * target;
            } else {
                return -1;
            }
        }
        return resSum / 2;
    }
}
